package cn.maihe.elg.operation.centers.fuzhou.handler.service;

import cn.maihe.elg.operation.centers.fuzhou.config.FZMakeInfoConfig;
import cn.maihe.elg.operation.centers.fuzhou.dto.handler.FZCheckGuaranteeReqDTO;
import cn.maihe.elg.operation.centers.fuzhou.dto.handler.FZCheckGuaranteeRespDTO;
import cn.maihe.elg.operation.centers.fuzhou.dto.handler.vo.FZBondCheckDTO;
import cn.maihe.elg.operation.centers.fuzhou.dto.handler.vo.FZBondCheckItemDTO;
import cn.maihe.elg.operation.centers.fuzhou.enums.HyMakeCheckResultEnum;
import cn.maihe.elg.operation.centers.fuzhou.handler.FZCenterTradeService;
import cn.maihe.elg.operation.centers.supports.CenterTradeContext;
import cn.maihe.elg.operation.centers.supports.annotation.CenterTradeService;
import cn.maihe.elg.operation.model.enums.AcceptOrderStatus;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.model.enums.CenterProviderEnum;
import cn.maihe.elg.operation.model.enums.TradeType;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.repository.entity.GuaranteeDO;
import cn.maihe.elg.operation.repository.entity.ProjectInfoDO;
import cn.maihe.elg.operation.service.business.ProjectInfoService;
import cn.maihe.elg.operation.service.guarantee.GuaranteeService;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.service.order.ApplyOrderInfoService;
import cn.maihe.elg.operation.service.order.RefundOrderInfoService;
import cn.maihe.elg.operation.supports.guarantee.enums.RespCode;
import cn.maihe.elg.operation.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * @Description 保函查验申请 2.7
 * @Author alan
 * @Date 2025/02/25.
 */
@CenterTradeService(
        provider = CenterProviderEnum.ESB_FUZHOU,
        centerNo = CenterNoEnum.FUZHOU,
        tradeType = TradeType.inspection,
        reqClass = FZCheckGuaranteeReqDTO.class,
        respClass = FZCheckGuaranteeRespDTO.class
)
@Slf4j
public class FZCheckGuaranteeService extends FZCenterTradeService<FZCheckGuaranteeReqDTO, FZCheckGuaranteeRespDTO> {

    @Resource
    ApplyOrderInfoService applyOrderInfoService;
    @Resource
    AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    RefundOrderInfoService refundOrderInfoService;

    @Autowired
    private ProjectInfoService projectInfoService;

    @Resource
    private GuaranteeService guaranteeService;

    public FZCheckGuaranteeService(FZMakeInfoConfig centerInfosConfig) {
        super(centerInfosConfig);
    }


    /**
     * 具体业务处理
     *
     * @param context
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public FZCheckGuaranteeRespDTO processService(CenterTradeContext<FZCheckGuaranteeReqDTO, FZCheckGuaranteeRespDTO> context) {

        FZCheckGuaranteeReqDTO req = context.getReq();
        Long centerId = context.getCenterId();
        Long orgId = context.getOrgId();

        Assert.hasText(req.getSerialNum(), () -> "serialNum不能为空");
        GuaranteeDO guaranteeDO = guaranteeService.getGuaranteeBySerialNum(req.getSerialNum());

        AcceptOrderInfoDO orderInfo = acceptOrderInfoService.getByGuaranteeNo(centerId, orgId, guaranteeDO.getGuaranteeNo());
        if ((orderInfo == null) || (orderInfo.getStatus() < AcceptOrderStatus.HAS_OUT_GUARANTEE.getStatus())) {
            FZCheckGuaranteeRespDTO result = FZCheckGuaranteeRespDTO.success("查验失败", FZCheckGuaranteeRespDTO.class);
            result.setCode(RespCode.INSPECT_NO.getRespCode());
            return result;
        }

        ProjectInfoDO projectInfoDO = projectInfoService.selectById(orderInfo.getProjectId());
//        GuaranteeDO guarantee = guaranteeService.getGuaranteeByAcceptOrderNo(orderInfo.getAcceptOrderNo());

        FZBondCheckDTO fzBondCheckDTO = new FZBondCheckDTO();
        fzBondCheckDTO.setGuaranteeNo(req.getGuaranteeNo());
        fzBondCheckDTO.setSerialNum(guaranteeDO.getSerialNum());
        fzBondCheckDTO.setKtype(4);
        fzBondCheckDTO.setWarrantee(orderInfo.getBidCustomerName());
        fzBondCheckDTO.setBeneficiary(projectInfoDO.getTenderer());
        fzBondCheckDTO.setProitem(projectInfoDO.getSectionName());
        // 保函品种，1投标保函
        fzBondCheckDTO.setVariety("1");
        fzBondCheckDTO.setGuaranteeStartDate(DateUtil.formateDate(guaranteeDO.getServiceBeginTime(), "yyyy-MM-dd"));
        fzBondCheckDTO.setGuaranteeEndDate(DateUtil.formateDate(guaranteeDO.getServiceEndTime(), "yyyy-MM-dd"));
        ;
        fzBondCheckDTO.setAmount(fen2yuanString(guaranteeDO.getBidBond()));
        // 是否转开，1是2否
        fzBondCheckDTO.setIsAvert("1");

        FZCheckGuaranteeRespDTO respDTO = FZCheckGuaranteeRespDTO.success("查验完成", FZCheckGuaranteeRespDTO.class);
        respDTO.setData(fzBondCheckDTO);
        respDTO.setStatus("200");
        return respDTO;
    }
}
